﻿using System.Collections.Generic;
using System.Linq;

namespace StudentManagement.Models
{
    public class MockStudentRepository : IStudentRepository
    {
        private List<Student> _studentlist;
        public MockStudentRepository()
        {
            _studentlist = new()
            {
                new() { Id = 1, Name = "张三", ClassName =ClassNameEnum.FistGrade, Email = "Tony@163.com" },
                new() { Id = 2, Name = "李四", ClassName =ClassNameEnum.SecondGrade, Email = "List@163.com" },
                new() { Id = 3, Name = "王二麻子", ClassName =ClassNameEnum.ThreeGrade, Email = "Wermazi@163.com" },
            };
        }

        public Student Add(Student student)
        {
          student.Id=_studentlist.Max(x => x.Id)+1;
            _studentlist.Add(student);
            return student;
        }

        public Student Delete(int id)
        {
            var student = _studentlist.FirstOrDefault(s => s.Id == id);
            if (student != null)
                _studentlist.Remove(student);
            return student;
        }

        public IEnumerable<Student> GetAllStudents()
        {
            return _studentlist;
        }

        public Student GetStudent(int id)
        {
            return _studentlist.FirstOrDefault(s => s.Id == id);
        }

        public Student Update(Student updateStudent)
        {
            var stu = _studentlist.FirstOrDefault(s => s.Id == updateStudent.Id);
            if (stu != null)
            {
                stu.Name = updateStudent.Name;
                stu.Email = updateStudent.Email;
                stu.ClassName = updateStudent.ClassName;
            }
            return stu;
        }
    }
}
